#import <Foundation/Foundation.h>
#import "Signup.h"
#import "Masonry.h"
#import "AFNetworking.h"

@implementation Signup

- (void)viewDidLoad{
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor colorWithRed:15/255.0 green:135/255.0 blue:219/255.0 alpha:1];
    //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundlogo.png"]];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"backgroundlogo"ofType:@"png"];

    UIImage *image = [UIImage imageWithContentsOfFile:path];

    self.view.layer.contents = (id)image.CGImage;
    
    self.username = [[UITextField alloc]init];
    self.username.borderStyle = UITextBorderStyleRoundedRect;
    self.username.autocapitalizationType=UITextAutocapitalizationTypeNone;
    [self.username setPlaceholder:@"用户名"];
    //[self.username becomeFirstResponder];
    [self.view addSubview:self.username];
    UIEdgeInsets padding = UIEdgeInsetsMake(150, 80, 0, 80);
    [self.username mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.top.equalTo(self.view.mas_top).with.offset(padding.top);
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
        make.width.mas_equalTo(self.view.frame.size.width * 0.75);
        make.height.mas_equalTo(40);
    }];
    
    self.password = [[UITextField alloc]init];
    self.password.borderStyle = UITextBorderStyleRoundedRect;
    self.password.secureTextEntry = YES;
    self.password.autocapitalizationType=UITextAutocapitalizationTypeNone;
    [self.password setPlaceholder:@"密码"];
    [self.view addSubview:self.password];
    padding = UIEdgeInsetsMake(30, 80, 0, 80);
    [self.password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.username.mas_bottom).with.offset(padding.top);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(self.view.frame.size.width * 0.75);
        make.height.mas_equalTo(40);
    }];
    
    self.signupbtn = [[UIButton alloc]init];
    self.signupbtn.backgroundColor = [UIColor colorWithRed:22/255.0 green:119/255.0 blue:230/255.0 alpha:1];
    [self.signupbtn setTitle:@"注册" forState:(UIControlStateNormal)];
    self.signupbtn.titleLabel.font = [UIFont systemFontOfSize: 24];
    self.signupbtn.layer.cornerRadius = 10;
    self.signupbtn.layer.masksToBounds = YES;
    [self.signupbtn addTarget:self action:@selector(SignupClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.signupbtn];
    padding = UIEdgeInsetsMake(30, 120, 0, 120);
    [self.signupbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.password.mas_bottom).with.offset(padding.top);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(self.view.frame.size.width * 0.75);
        make.height.mas_equalTo(40);
    }];
    
}

// 点击登录
- (void) SignupClick{
    NSString *username = self.username.text;
    NSString *password = self.password.text;
    NSLog(@"username:%@ password:%@", username, password);
    if(username.length == 0 || password.length == 0){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录失败" message:@"用户名或密码为空" preferredStyle: UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                return;
            }]];
        [self presentViewController:alert animated:YES completion:nil];
    }

    NSString *URLString = @"http://47.107.247.52:8080/user/signup";
    // zhangsan@sysu.edu.cn
    NSDictionary *parameters = @{
        @"username": self.username.text,
        @"password": self.password.text
    };
    NSLog(@"%@", parameters);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager POST:URLString parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
            NSLog(@"%@", responseObject);
            // 判断字典字段是否为success
            if([responseObject[@"ok"] isEqual: @1]){
                [self dismissViewControllerAnimated:YES completion:nil];
            }else{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册失败" message:@"该用户已存在" preferredStyle: UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        return;
                    }]];
                [self presentViewController:alert animated:YES completion:nil];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败");
        }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES]; //实现该方法是需要注意view需要是继承UIControl而来的
}
@end

